package cn.ffcs.uec.post.component;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModelList;

import cn.ffcs.uec.organization.model.UecOrgDept;
import cn.ffcs.uec.post.component.bean.UecPostListboxExtBean;
import cn.ffcs.uec.post.manager.UecPostManager;
import cn.ffcs.uec.post.model.UecPost;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.NumericUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import lombok.Getter;
import lombok.Setter;

@Controller
@Scope("prototype")
public class UecPostListboxExt extends Div implements IdSpace {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2987104426987882007L;

	private UecPostListboxExtBean bean = new UecPostListboxExtBean();
	/**
	 * zul.
	 */
	private static final String zul = "/pages_nmg/uec_post/comp/uec_post_listbox_ext.zul";

	private UecPostManager uecPostManager = (UecPostManager) ApplicationContextUtil
			.getBean("uecPostManager");

	private UecPost uecPost;

	private UecPost eventUecPost;

	private UecOrgDept uecOrgDept;

	@Getter
	private Boolean isBandbox = false;

	/**
	 * @param isBandbox
	 *            the isBandbox to set
	 */
	public void setIsBandbox(Boolean isBandbox) {
		this.isBandbox = isBandbox;

		if (this.isBandbox = true) {
			onQuery();
		}
	}

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecPostListboxExt() {
		// 1.绑定页面
		Executions.createComponents(zul, this, null);
		// 2.页面控件与java属性绑定
		Components.wireVariables(this, bean);
		// 3.绑定监听事件
		Components.addForwards(this, this, '$');

		// 接受抛出的查询事件
		this.addEventListener("onOrgPostQuery", new EventListener() {
			public void onEvent(Event event) throws Exception {
				eventUecPost = (UecPost) event.getData();
				onQuery();
			}
		});
		this.addForward("onUecDeptSelect", this, "onUecDeptSelectResponse");
		this.addForward("onCleanUecDeptSelect", this, "onCleanUecDeptSelectRseponse");
		
		onQuery();
	}

	/**
	 * 触发选中事件
	 */
	public void onSelectRequest() throws Exception {
		if (bean.getUecPostListbox().getSelectedCount() > 0) {
			uecPost = (UecPost) bean.getUecPostListbox().getSelectedItem()
					.getValue();
			Events.postEvent("onSelectUecPost", this, uecPost);
		} else {
			Events.postEvent("onSelectNoUecPost", this, null);
		}
	}
	
	/**
	 * 监听传过来的事件
	 * @param event
	 */
	public void onUecDeptSelectResponse(ForwardEvent event) {
		if (event.getOrigin() != null) {
			uecOrgDept = (UecOrgDept) event.getOrigin().getData();
			eventUecPost = UecPost.newInstance();
			eventUecPost.setPkDept(uecOrgDept.getPkDept());
			onQuery();
		}
	}
	
	/**
	 * 清理变量事件
	 * @param event
	 */
	public void onCleanUecDeptSelectRseponse(ForwardEvent event) {
		if (event.getOrigin() == null) {
			clearVariable();
		}
	}

	/**
	 * 查询
	 */
	public void onQuery() {
		uecPost = new UecPost();
		if (eventUecPost != null) {
			uecPost.setPkOrg(eventUecPost.getPkOrg());
			uecPost.setPkDept(eventUecPost.getPkDept());
		}
		uecPost.setPostname(bean.getPostname().getValue());
		this.queryUecPost();
	}

	/**
	 * 查询
	 */
	public void queryUecPost() {
		int activePage = bean.getUecPostListboxPaging().getActivePage();
		int pageSize = bean.getUecPostListboxPaging().getPageSize();
		PageInfo pageInfo = uecPostManager.queryPageInfoByUecPost(uecPost,
				activePage + 1, pageSize);
		ListModelList dataList = new BindingListModelList(
				pageInfo.getDataList(), true);
		//总条数
		bean.getTotalSize().setValue(String.valueOf(pageInfo.getTotalCount()));
		bean.getUecPostListbox().setModel(dataList);
		bean.getUecPostListboxPaging().setTotalSize(
				NumericUtil.nullToZero(pageInfo.getTotalCount()));
	}

	/**
	 * 重置
	 */
	public void onReset() {
		bean.getPostname().setValue(null);
		bean.getTotalSize().setValue(null);
		clearVariable();
		ListboxUtils.clearListbox(this.bean.getUecPostListbox());
		this.bean.getUecPostListboxPaging().setTotalSize(1);
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecPost = null;
		eventUecPost = null;
	}

	/**
	 * 翻页
	 */
	public void onListboxPaging() {
		this.queryUecPost();
	}

}
